import { defineStore } from 'pinia'
import { UserState } from '@/stores/interface'
import piniaPersistConfig from '@/config/piniaPersist'

export const useUserStore = defineStore({
  id: 'user-store',
  state: (): UserState => ({
    token: '',
    tokenExpired: 0,
    userInfo: { name: 'mo' }
  }),
  getters: {},
  actions: {
    setToken(token: string) {
      this.token = token
    },
    setTokenExpired(tokenExpired: number) {
      this.tokenExpired = tokenExpired
    },
    setUserInfo(userInfo: UserState['userInfo']) {
      this.userInfo = userInfo
    },
    isTokenExpired() {
      return this.tokenExpired - new Date().getTime() < 0
    },
    resetState() {
      this.token = ''
      this.tokenExpired = 0
      this.userInfo = {}
    }
  },
  persist: piniaPersistConfig('user-store')
})
